﻿using Zhaoxi.ReadingPlatform.WebSiteApi.Utility.SwaggerExt;

internal static class CustomSwaggerExt
{
    //public static void AddSwaggerExt(IServiceCollection services)
    /// <summary>
    /// 这是“扩展方法”的写法
    /// </summary>
    /// <param name="services"></param>
    public static void AddSwaggerExt(this IServiceCollection services)
    {
        services.AddControllers();
        services.AddEndpointsApiExplorer();
        services.AddSwaggerGen(options =>
        {
            #region API版本
            typeof(ApiVersion).GetEnumNames().ToList().ForEach((version) =>
            {
                options.SwaggerDoc(version, new Microsoft.OpenApi.Models.OpenApiInfo()
                {
                    Title = "读书平台API文档",
                    Version = version,
                    Description = $"通用版本的coreapi版本-{version}"
                });
            });
            #endregion

            #region 显示api注释
            // xml文件绝对路劲
            // 控制器api会生成xml文件
            var file = Path.Combine(AppContext.BaseDirectory, "Zhaoxi.ReadingPlatform.WebSiteApi.xml");

            // 显示控制器层注释, true
            options.IncludeXmlComments(file, true);

            // 对action的名称排序
            options.OrderActionsBy(o => o.RelativePath);
            #endregion
        });

    }

    //public static void UseSwaggerExt(WebApplication app)
    public static void UseSwaggerExt(this WebApplication app)
    {
        if (app.Environment.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
                {
                    foreach (var version in typeof(ApiVersion).GetEnumNames())
                    {
                        c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"读书平台【{version}】版本");
                    }
                });
        }
    }
}